package com.zz.yt.lib.bluetooth.base;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.inuker.bluetooth.library.BluetoothClient;
import com.inuker.bluetooth.library.connect.listener.BluetoothStateListener;
import com.zz.yt.lib.bluetooth.R;
import com.zz.yt.lib.ui.CustomTitleBar;
import com.zz.yt.lib.ui.base.ui.LatteTitleDelegate;

import java.util.concurrent.TimeUnit;


/**
 * 未连接设备
 *
 * @author qf
 * @version 1.0
 **/
public class DeviceNoDelegate extends LatteTitleDelegate {

    /**
     * 蓝牙是否打开
     */
    private boolean isResult;
    /**
     * 打开蓝牙
     */
    private BluetoothClient mClient = null;
    /**
     * 蓝牙状态监听
     */
    private BluetoothAdapter mBluetooth = null;

    @NonNull
    public static DeviceNoDelegate create() {
        final Bundle args = new Bundle();
        final DeviceNoDelegate fragment = new DeviceNoDelegate();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public Object setLayout() {
        return R.layout.blu_delegate_device_no;
    }

    @Override
    protected void setCustomTitleBar(@NonNull CustomTitleBar titleBar) {
        titleBar.setText("未连接连接提示");
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        super.onBindView(savedInstanceState, rootView);
        initBluetooth();
    }

    private void initBluetooth() {
        mBluetooth = BluetoothAdapter.getDefaultAdapter();
        mClient = Bluetooth.getRestService();
        //蓝牙开关监听
        mClient.registerBluetoothStateListener(new BluetoothStateListener() {

            @Override
            public void onBluetoothStateChanged(boolean openOrClosed) {
                hideLoading();
            }
        });
        onClicked();
    }

    /**
     * 循环判断蓝牙状态，如果是打开关闭蓝牙界面
     */
    private void inReadThread() {
        ThreadUtils.executeByFixedAtFixRate(1, new ThreadUtils.SimpleTask<Boolean>() {

            @Override
            public Boolean doInBackground() {
                return mBluetooth.isEnabled();
            }

            @Override
            public void onSuccess(@Nullable Boolean result) {
                isResult = result;
                if (isResult) {
                    cancel();
                    onBackPressedSupport();
                }
            }
        }, 1, TimeUnit.SECONDS);
    }

    /**
     * 提示开启蓝牙
     */
    private void onClicked() {
        inReadThread();
        showLoading("正在打开蓝牙，请稍等");
        if (mClient != null) {
            mClient.openBluetooth();
        }
    }


    @Override
    public boolean onBackPressedSupport() {
        if (!isResult) {
            ToastUtils.showShort("请打开蓝牙");
            return false;
        }
        getSupportDelegate().setFragmentResult(Activity.RESULT_OK, null);
        getSupportDelegate().pop();
        return true;
    }

}
